<?php

namespace App\Models\System;

use App\Models\User;
use Illuminate\Database\Eloquent\Model;

/**
 * 系统操作日志模型
 *
 * Date: 2018/6/15
 * @author Sam
 * @package App\Models\System
 */
class SysLog extends Model
{
	/**
	 * 定义数据表名称
	 *
	 * @var string
	 * Date: 2018/6/15
	 * @author Sam
	 */
	protected $table = 'logs';

	/**
	 * 定义不可填充字段
	 *
	 * @var array
	 * Date: 2018/6/15
	 * @author George
	 */
	protected $guarded = [
		'id',
	];

	/**
	 * 写入日志
	 *
	 * Date: 2018/6/15
	 * @author Sam
	 * @param $user
	 * @param $module
	 * @param $info
	 */
	public static function log($user, $module, $info)
	{
		$attributes = [
			'user_id' => $user->id,
			'name' => $user->name,
			'info' => $info,
			'module' => $module,
		];

		if ($user instanceof User) {
			$attributes['company_id'] = $user->company_id;
			$attributes['company'] = optional($user->company)->title;
		}

		self::create($attributes);
	}

}
